/*
 * @Author: pmy
 * @Date: 2022-09-22 10:28:21
 * @LastEditors: pmy
 * @LastEditTime: 2022-09-22 11:10:14
 * @FilePath: \tyzhhw_yidong\src\assets\js\utils.js
 * @Description: js工具包
 * Copyright (c) 2022 by pmy/exsun, All Rights Reserved. 
 */

//防抖函数
export function debounce(fnName, delay) {
    let timer
    return function () {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            this[fnName]();
        }, delay)
    }
}

//节流函数：n段时间内只执行第一次
export function throttle(fn, delay) {
    let flag = true;
    return function (args) {
        if (flag) {
            setTimeout(() => {
                fn.call(this, args);
                flag = true;
            }, delay)
        }
        flag = false;
    }
}